<?php
/**
 * Cube PHP Framework
 * 
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 * 
 * @author Dillen / Steffen
 */

namespace Library\Mvc;

class View extends \Library\Web\WebApplication
{
	/**
	 * Application configuration
	 * 
	 * @property Array
	 */
	private $_config;

	/**
	 * View memory
	 * 
	 * @property {Array} $_memory
	 * @protected
	 */
	protected $_memory = array();
	
	/**
	 * Constructor
	 */
	public function __construct()
	{
        parent::__construct();
        
		$this->_config = \Library\Platform::getConfig();
        
        $this->_memory['cookies'] = $this->cookies;
        $this->_memory['response'] = $this->response;
        $this->_memory['sessions'] = $this->sessions;
        $this->_memory['post'] = $this->post;
        $this->_memory['get'] = $this->get;
	}
	
	/**
	 * Assign memory to the view
	 * 
	 * @param {String} $key
	 * @param {Mixed} $value
	 */
	public function assign($key, $value = null)
	{
		if (is_array($key))
		{
			foreach ($key as $key => $value)
			{
				$this->assign($key, $value);
			}
		}
		else
		{
			$this->_memory[$key] = $value;
		}
	}
	
	/**
	 * Render view
	 * 
	 * @method render
	 * @param {String} $view
	 */
	public function render($view, array $memory = array(), $path = 'views')
	{
		$this->assign($memory);
		
		$path = $this->_config['application']['path'] . $path . '/' . $view;
		
		if (!file_exists($path))
		{
			throw new \Library\IO\Exception\FileNotFoundException('View not found ' . $path);
		}
		
		include $path;
	}
	
	/**
	 * Magic method
	 * 
	 * @method __get
	 * @param {String} $key
	 */
	public function __get($key)
	{
		return isset($this->_memory[$key]) ? $this->_memory[$key] : null;
	}
}
